import store from '@/store'
let baseConfig = {
	baseUrl: "/api",
	version: 'v1',
	data: {},
	filePath: '',
	name: "file",
	method: "get",
	autoError: true,
	loading: true,
	header: {
		'Accept': 'application/json',
		"from":'wx',
	},
}
const service = (data) => {
	let config = JSON.parse(JSON.stringify(baseConfig))
	config.header.Authorization = "Bearer " + store.state.$token
	for (let key in data) {
		config[key] = data[key]
	}
	if (config.loading) {
		uni.showLoading({
			title: '上传中'
		});
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.baseUrl + '/' + config.version + '/' + config.url,
			data: config.data,
			filePath: config.filePath,
			name: config.name,
			method: config.method,
			header: config.header,
			success: (res) => {
				if (config.autoError) {
					if (!res.data) {
						uni.showToast({
							title: res.message,
							duration: 2000,
							icon: "none"
						});
					} else {
						resolve(res.data)
					}
				} else {
					resolve(res.data)
				}
			},
			fail: (error) => {
				console.error(error)
				if (config.loading) {
					uni.showToast({
						title: '上传失败',
						duration: 2000,
						icon: "none"
					});
				}
				reject(error);
			},
			complete: () => {
				uni.hideLoading();
			}
		});
	})
}
export default service
